//
//    Copyright (C) 2012  OinkStudio
//
//    This program is free software: you can redistribute it and/or modify
//    it under the terms of the GNU General Public License as published by
//    the Free Software Foundation, either version 3 of the License, or
//    (at your option) any later version.
//
//    This program is distributed in the hope that it will be useful,
//    but WITHOUT ANY WARRANTY; without even the implied warranty of
//    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//    GNU General Public License for more details.
//
//    You should have received a copy of the GNU General Public License
//    along with this program.  If not, see <http://www.gnu.org/licenses/>.
package game;

public class RuleEqualChain extends Rule {

	private static Rule ruleSingleton;

	protected RuleEqualChain(String ruleName, int priority) {
		super(ruleName, priority);
	}

	public void check(int row, int column, Game game) {
		Obolo[][] board = game.getBoard();
		// TODO Auto-generated method stub

		boolean south = false;
		boolean north = false;
		boolean west = false;
		boolean east = false;
		int[] compareSouth = checkSouth(row, column, game);
		int[] compareNorth = checkNorth(row, column, game);
		int[] compareWest = checkWest(row, column, game);
		int[] compareEast = checkEast(row, column, game);
		if (compareNorth == null) {
			compareNorth = new int[2];
			compareNorth[0] = 20;
			compareNorth[1] = 21;
		}
		if (compareSouth == null) {
			compareSouth = new int[2];
			compareSouth[0] = 21;
			compareSouth[1] = 22;
		}
		if (compareWest == null) {
			compareWest = new int[2];
			compareWest[0] = 22;
			compareWest[1] = 23;
		}
		if (compareEast == null) {
			compareEast = new int[2];
			compareEast[0] = 23;
			compareEast[1] = 24;
		}

		if ((compareSouth[0] == compareSouth[1])
				&& compareNorth[0] == compareNorth[1]) {
			south = true;
			north = true;
		}
		if ((compareSouth[0] == compareSouth[1])
				&& compareWest[0] == compareWest[1]) {
			south = true;
			west = true;
		}
		if ((compareSouth[0] == compareSouth[1])
				&& compareEast[0] == compareEast[1]) {
			south = true;
			east = true;
		}
		if ((compareNorth[0] == compareNorth[1])
				&& compareEast[0] == compareEast[1]) {
			north = true;
			east = true;
		}
		if ((compareNorth[0] == compareNorth[1])
				&& compareWest[0] == compareWest[1]) {
			north = true;
			west = true;
		}
		if ((compareEast[0] == compareEast[1])
				&& compareWest[0] == compareWest[1]) {
			east = true;
			west = true;
		}
		if (east) {
			board[row][column + 1].setOwner(board[row][column].getOwner());
			putOboloChain(row, column + 1, board[row][column + 1], game);
		}
		if (west) {
			board[row][column - 1].setOwner(board[row][column].getOwner());
			putOboloChain(row, column - 1, board[row][column - 1], game);
		}
		if (north) {
			board[row - 1][column].setOwner(board[row][column].getOwner());
			putOboloChain(row - 1, column, board[row - 1][column], game);
		}
		if (south) {
			board[row + 1][column].setOwner(board[row][column].getOwner());
			putOboloChain(row + 1, column, board[row + 1][column], game);
		}

	}

	public static Rule getInstance() {
		if (ruleSingleton == null)
			ruleSingleton = new RuleEqualChain("RuleEqualChain", 9);
		return ruleSingleton;
	}

	public void putOboloChain(int row, int column, Obolo obolo, Game game)
			throws IllegalArgumentException {
		Obolo[][] board = game.getBoard();
		board[row][column] = obolo;
		game.checkRules(row, column);

	}

}
